package fair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import fair.pojo.User;
import fair.mapper.UserMapper;
import fair.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 用户信息 服务实现类
 * </p>
 *
 * @author fair
 * @since 2021-03-04
 */
@Slf4j
@Transactional
@Service(interfaceClass = IUserService.class, version = "${dubbo.service.core.version}", group = "${dubbo.service.core.group}", retries = -1)
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    public User findOne(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.lambda().eq(User::getUsername, username);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer insert(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public List<User> findTeaByPage(String pageNum, String pageSize) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getRole,"teacher");
        IPage<User> page = new Page<>();
        page.setCurrent(Integer.parseInt(pageNum));
        page.setSize(Integer.parseInt(pageSize));
        page = userMapper.selectPage(page, queryWrapper);
        List<User> users = page.getRecords();
        return users;
    }

    @Override
    public List<User> findStuByPage(String pageNum, String pageSize) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getRole,"student");
        IPage<User> page = new Page<>();
        page.setCurrent(Integer.parseInt(pageNum));
        page.setSize(Integer.parseInt(pageSize));
        page = userMapper.selectPage(page, queryWrapper);
        List<User> users = page.getRecords();
        return users;
    }

    public void updateByUsername(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUsername, user.getUsername());
        userMapper.update(user, queryWrapper);
    }
}
